﻿using Libs.Content;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Video_Detail : System.Web.UI.Page
{
    private readonly int CateID = int.Parse(ConfigurationManager.AppSettings["VIDEO.CATE"].ToString());
    protected string UrlImage = "";
    protected string UrlYoutube = "";
    protected int cateID = 0, newsID = 0;
    private const int PerPages = 12;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }
        List<NewsWeb> listVideo;
        NewsWeb _NewsWeb = new NewsWeb();
        News _News = new News();
        NewsCategories _Cate = new NewsCategories();
        _Cate = _Cate.Get(CateID);
        if (newsID == 0)
        {
            listVideo = _NewsWeb.GetList(CateID, 0, 1);
            _News = new News().Get(listVideo[0].NewsID);
        }
        else
        {
            _News = new News().Get(newsID);
        }

        hlMainVideo.InnerText = _News.Title;
        ltContent.Text = _News.Content;
        UrlYoutube = _News.SubTitle;
        UrlImage = AppUtils.ImageUrl(_News.ImageUrl, "650", "355");
        listVideo = _NewsWeb.GetList(CateID, newsID, PerPages);
        ltrTags.Text = Tags(_News.Tags);
        if (listVideo.Count > 0)
        {
            rptNewVideo.DataSource = listVideo;
            rptNewVideo.DataBind();

            if (listVideo.Count == PerPages)
            {
                if (CateID != 0)
                {
                    hlnext.HRef = NewsUtils.CateVideoLink(CateID.ToString(), _Cate.Url, listVideo[listVideo.Count - 1].NewsID.ToString());
                }
                else
                {
                    hlnext.HRef = NewsUtils.CateVideoLink("0", _Cate.Url, listVideo[listVideo.Count-1].NewsID.ToString());
                }
            }
            else
            {
                hlnext.Visible = false;
            }
        }

        rptMostView.DataSource = _NewsWeb.GetList(CateID, 0, 4);
        rptMostView.DataBind();
    }
    private string Tags(string tags)
    {
        if (string.IsNullOrEmpty(tags)) return "";
        string[] list = tags.Split(',');

        string s = "";
        for (int i = 0; i < list.Length; i++)
        {
            list[i] = list[i].Trim();
            s = s + string.Format("<a href=\"{0}\">{1}</a>", Constant.HOME_PATH + "search.html?q=" + list[i], list[i]) + ", ";
        }
        return s;
    }
}